package com.pignest.hotel.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

import com.pignest.hotel.beans.User;
import com.pignest.hotel.beans.global.Menu;
import com.pignest.hotel.system.P;
import com.pignest.hotel.system.Permission;

public class MenuLoader implements InitializingBean {
	
	private static final String MENU_MAIN = "main";
	private static final String MENU_setting = "setting";
	private static final String MENU_admin = "admin";
	private static final String MENU_REPORT = "report";
	
	private HashMap<String, List<Menu>> menus;
	
	private List<Menu> getMenus(String type, User user) {
		List<Menu> ms = menus.get(type);
		List<Menu> r = new ArrayList<Menu>();
		P p = P.get();
		for (Menu menu : ms) {
			String url = menu.getUrl();
			String[] permissions = p.getPermissions(url);
			if (Permission.alow(permissions, user))
				r.add(menu);
		}
		return r;
	}

	public List<Menu> getMainMenus(User user) {
		return getMenus(MENU_MAIN, user);
	}
	
	public List<Menu> getSettingMenus(User user) {
		return getMenus(MENU_setting, user);
	}
	
	public List<Menu> getAdminMenus(User user) {
		return getMenus(MENU_admin, user);
	}
	
	public List<Menu> getReportMenus(User user) {
		return getMenus(MENU_REPORT, user);
	}
	
	/////////////////////////////////////////////////////////////////////////////////////
	
	private Resource  configLocation;
	
	public void setConfigLocation(Resource  configLocation) {
		this.configLocation = configLocation;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public void afterPropertiesSet() throws Exception {
		if (configLocation == null)
			throw new RuntimeException("configLocation not setted!");
		menus = new HashMap<String, List<Menu>>();
		try {
			InputStream is = configLocation.getInputStream();
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(is);
			Element root = document.getRootElement();
			for (Iterator<Element> it = root.elementIterator(); it.hasNext();) {
				Element group = it.next();
				if (!"group".equals(group.getName()))
					continue;
				String name = group.attributeValue("name");
				List<Menu> ms = menus.get(name);
				if (ms == null) {
					ms = new ArrayList<Menu>();
					menus.put(name, ms);
				}
				for (Iterator<Element> iter = group.elementIterator(); iter.hasNext();) {
					Element e = iter.next();
					Menu menu = new Menu();
					menu.setName(e.attributeValue("name"));
					menu.setUrl(e.attributeValue("path"));
//					menu.setPermissions(e.attributeValue("permission"));
					menu.setIcon(e.attributeValue("icon"));
					menu.setParameter(e.attributeValue("parameter"));
					ms.add(menu);
				}
			}
		} catch (DocumentException e) {
			throw new FatalBeanException(
					"Property [configLocation] of class parse error.");
		}
	}

}
